package hlhj.fhp.supreme.customview
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.JiePanCatBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.choose_jiepan_dialog.*
/**
 * Created by Never Fear   on 2018\7\18 0018.
Never More....
 */
class ChooseJiePanDialog(private val c: Context,private val listener:OnSelectListener,private val activity: Activity) :Dialog(c, R.style.CustomDialog) {
    private lateinit var adp:BaseQuickAdapter<JiePanCatBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<JiePanCatBean.DataBean>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.choose_jiepan_dialog)

        adp=object :BaseQuickAdapter<JiePanCatBean.DataBean,BaseViewHolder>(R.layout.choose_jiepan_item,datas){
            override fun convert(helper: BaseViewHolder?, item: JiePanCatBean.DataBean?) {
                helper?.setText(R.id.itemTv,item?.name)
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(c,LinearLayoutManager.VERTICAL,false)
        adp.setOnItemClickListener { adapter, view, position ->
            listener.yes(datas[position].name,datas[position].id.toString())
            dismiss()
        }

        OkGo.post<JiePanCatBean>(Urls.BaseUrl+Urls.JIEPAN_CAT)
                .execute(object :JsonCallBack<JiePanCatBean>(activity){
                    override fun onSuccess(response: Response<JiePanCatBean>?) {
                        val panCatBean = response?.body()
                        if (panCatBean?.code==1){
                            datas.addAll(panCatBean.data)
                            adp.notifyDataSetChanged()
                            this.toast(c,"111")
                        }
                    }
                })
    }

    interface OnSelectListener{
        fun yes(str:String,id:String)
    }
}